using System;
using System.Collections.Generic;
using Com.ContentRendering.Api.DataEngine;
using ContentRenderingApi.Configuration;

namespace ContentRenderingApi.Data
{
    /// <summary/>
    internal class ChildData : AbstractData
    {
        private string _name;
        private object _value;
        private int _position;
        private int _length;

        /// <summary/>
        internal ChildData(
            ReflectionCache reflectionCache,
            TransformationConfiguration transformationConfiguration,
            bool isCaching,
            AbstractData parent,
            string name,
            object value,
            int length) 
            : base(
            reflectionCache,
            transformationConfiguration,
            isCaching,
            parent) 
        {
            this._name = name;
            this._value = value;
            this._length = length;
        }
        
        /// <summary/>
        protected override void ResolveChildData(List<object> values, string name)
        {
            // find the child data of the current data for the given name
            object newValue = ChildReflection.Translate(
                base.ReflectionCache,
                this._value,
                name);

            // convert the object into a usable value
            IEnumerator<object> enumerator = SelfReflection.Translate(
                base.ReflectionCache,
                newValue);

            // move each element to the new list
            while (enumerator.MoveNext())
            {
                values.Add(enumerator.Current);
            }
        }

        /// <summary/>
        protected override string Name
        {
            get { return this._name; }
        }

        /// <summary/>
        internal override object RawValue
        {
            get { return this._value; }
        }

        /// <summary/>
        internal override int Length
        {
            get { return this._length; }
        }

        /// <summary/>
        internal override int Position
        {
            get { return this._position; }
        }

        /// <summary/>
        protected override void AssignPosition(int position)
        {
            this._position = position;
        }
    }
}
